<script>
	import store from "@/store/index.js";
	// #ifdef APP
	// import {
	// 	commonLisenerPermissiong
	// } from "@/common/utils.js"
	import {
		unregisterRequestPermissionTipsListener
	} from "@/uni_modules/uni-registerRequestPermissionTips";
	import {
		friendsRequest,
		messageUnreadNum
	} from "@/common/api/index";
	const linAlive = uni.requireNativePlugin("Lin-Alive");
	// #endif
	export default {
		globalData: {
			timer: null,
		},
		onLaunch: function() {
			// console.log("App Launch");
			store.commit("getSystemInfo");
			// #ifdef APP
			linAlive.startAliveService({
					title: "保持位置信息",
					description: "持续获取位置信息以记录轨迹等信息。",
					hideFromTaskList: false,
					sendStatusWhenScreenOn: true,
					sendStatusScreenOnTime: 60,
					enableWakeLock: false,
					wakeLockScreenTime: 1,
				},
				(res) => {
					console.log(res);
				}
			);
			// commonLisenerPermissiong();
			// #endif
		},
		onShow: function() {
			// this.timer = setInterval(() => {
			//   // console.log(true);
			//   console.log(this.$store.state.mqtt.connected);
			// }, 2000);
			/*  这个消息只要一次  后端  */
			uni.$on("showError", async (res) => {
				await this.get_unreadNum();
				await this.showError(res);
			});
			uni.$on("add_friend", async (res) => {
				await this.showMsg(res);
				await this.get_unreadNum();
			});
			uni.$on("danger_alarm", async (res) => {
				await this.danger_alarm(res);
				await this.get_unreadNum();
			});
			// this.get_unreadNum();
		},
		onHide: function() {
			if (this.timer) {
				clearInterval(this.timer);
			}
			// this.timer = setInterval(() => {
			// 	// console.log(this.$store.state.mqtt.connected);
			// 	console.log(true);
			// }, 2000)
		},
		onExit: function() {
			this.$store.commit("disconnectMqtt");
			unregisterRequestPermissionTipsListener(null);
		},
		methods: {
			showMsg(value) {
				uni.showModal({
					title: "好友请求",
					content: `用户${value.nickname},手机号：${value.mobile} 于${value.time} 请求添加好友，请确认手机号、ID昵称无误后再添加！！！`,
					cancelText: "拒绝",
					confirmText: "同意",
					showCancel: false,
					success: async function(res) {
						if (res.confirm) {
							let formData = {
								friend_id: value.friend_id,
								status: 1,
							};
							let res = await friendsRequest(formData);
							if (res.code === 1) {
								this.showMsg("已添加好友");
							} else {
								this.showMsg(res.msg);
							}
						} else if (res.cancel) {
							let formData = {
								friend_id: value.friend_id,
								status: 2,
							};
							let res = await friendsRequest(formData);
							if (res.code === 1) {
								this.showMsg("已拒绝添加");
							} else {
								this.showMsg(res.msg);
							}
						}
					},
				});
			},
			showError(value) {
				uni.showModal({
					title: "求救通知",
					content: `${value.content}`,
					confirmText: "已知晓",
					showCancel: false,
					success: async function(res) {
						if (res.confirm) {
							let formData = {
								friend_id: value.friend_id,
								status: 1,
							};
							let res = await friendsRequest(formData);
							if (res.code === 1) {
								this.showMsg("已添加好友");
							} else {
								this.showMsg(res.msg);
							}
						} else if (res.cancel) {
							let formData = {
								friend_id: value.friend_id,
								status: 2,
							};
							let res = await friendsRequest(formData);
							if (res.code === 1) {
								this.showMsg("已拒绝添加");
							} else {
								this.showMsg(res.msg);
							}
						}
					},
				});
			},
			danger_alarm(value) {
				uni.showModal({
					title: "围栏通知",
					content: `${value.content}`,
					confirmText: "已知晓",
					showCancel: false,
					success: async function(res) {
						if (res.confirm) {
							let formData = {
								friend_id: value.friend_id,
								status: 1,
							};
							let res = await friendsRequest(formData);
							if (res.code === 1) {
								this.showMsg("已添加好友");
							} else {
								this.showMsg(res.msg);
							}
						} else if (res.cancel) {
							let formData = {
								friend_id: value.friend_id,
								status: 2,
							};
							let res = await friendsRequest(formData);
							if (res.code === 1) {
								this.showMsg("已拒绝添加");
							} else {
								this.showMsg(res.msg);
							}
						}
					},
				});
			},
			async get_unreadNum() {
				let res = await messageUnreadNum();
				if (res.code == 1) {
					this.$store.commit("setMessageValue", res.data.unread_count);
					if (res.data.unread_count) {
						uni.setTabBarBadge({
							index: 2,
							text: `${res.data.unread_count}`,
						});
					} else {
						uni.removeTabBarBadge({
							index: 2,
						});
					}
				} else {
					this.showMsg(res.msg);
				}
			},
		},
	};
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";
	@import "@/assets/css/index.scss";
	@import "@/static/iconfont/iconfont.css";
</style>